package com.sino.operation.util;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.druid.sql.visitor.functions.Char;
import com.sino.operation.entity.AccountArrayNameAndClass;
import com.sino.operation.peripheral.esc.request.core.param.Core_4010_Listnm01Info_Param;
import com.sino.operation.peripheral.esc.request.core.param.Core_4010_Listnm02_Param;
import com.sino.operation.peripheral.esc.request.core.param.Core_4010_ListnmInfo_param;
import com.sino.operation.vo.current.FieldInformation;
import com.sino.operation.vo.current.InfoInput;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName AccountGetArrayUtil
 * @Description 对公活期获取数组信息
 * @Author 北陈
 * @Date 2021/11/7 10:22
 * @Version 1.0
 */
public class AccountGetArrayUtil {

    public static List<Map<String, Object>> getResult(InfoInput infoInput){
        List<Map<String, Object>> maps = new ArrayList<>();
        Map<String, Object> mapResult = new HashMap<>();
        //确定这个大容器中是否存在1、2等数字
        Map<Integer, Object> integerMap = new HashMap<>();
        List<FieldInformation> listFieldInformation = infoInput.getListFieldInformation();
        //先循环一个中是否循环字段
        listFieldInformation.forEach(fieldInformation -> {
            if (ObjectUtil.isNotNull(fieldInformation)){
                String englishName = fieldInformation.getEnglishName();
                if (ObjectUtil.isNotNull(englishName)&&ObjectUtil.isNotEmpty(englishName)){
                    String substring = englishName.substring(englishName.length() - 1);
                    char[] chars = substring.toCharArray();
                    int length = chars.length;
                    for (int i = 0; i < length; i++) {
                        int charValue=(int)chars[i];
                        //该字段是循环
                        if (charValue>=49&&charValue<=57){
                            int parseInt = Integer.parseInt(String.valueOf(chars[i]));
                            integerMap.put(parseInt,null);
                        }
                    }
                }
            }
        });
        //根据循环字段循环判断结果判断走哪一个
        int size = integerMap.size();
        if (size>0){
            //证明有循环组，则一个一个取出来并组装
            for (Map.Entry<Integer,Object> key : integerMap.entrySet()){
                Integer keyInteger = key.getKey();
                //用于存储每一个循环组的一套map字段名和值集合
                Map<String, Object> oneMapNameAndValue = new HashMap<>();
                listFieldInformation.forEach(fieldInformation -> {
                    if (ObjectUtil.isNotNull(fieldInformation)){
                        String englishName = fieldInformation.getEnglishName();
                        if (ObjectUtil.isNotNull(englishName)&&ObjectUtil.isNotEmpty(englishName)){
                            //如果存在当前数字
                            if (englishName.contains(String.valueOf(keyInteger))){
                                String substring = englishName.substring(0, englishName.lastIndexOf(String.valueOf(keyInteger)));
                                String fieldValue = fieldInformation.getFieldValue();
                                oneMapNameAndValue.put(substring,fieldValue);
                            }

                        }
                    }
                });
                maps.add(oneMapNameAndValue);
            }
        }else {
            //则直接取出来，加入集合中
            listFieldInformation.forEach(fieldInformation -> {
                if (ObjectUtil.isNotNull(fieldInformation)){
                    String englishName = fieldInformation.getEnglishName();
                    if (ObjectUtil.isNotNull(englishName)&& StringUtils.isNotBlank(englishName)){
                        mapResult.put(englishName,fieldInformation.getFieldValue());
                    }
                }
            });
            maps.add(mapResult);
        }
        return maps;
    }

    /**
     * @Description: ${从报文中取出数组，对公活期建立客户信息}
     * param [infoInput, map]
     * return void
     * author 北陈
     * date 2021/11/7 12:18
    */
    public static void addCoreNewAccountByPublic(List<InfoInput> infoInputs,Map mapPost){

        Map<String, AccountArrayNameAndClass> nameValueMap = new HashMap<>();
        //地址信息
        AccountArrayNameAndClass list4010Nm01=AccountArrayNameAndClass.builder().listName("listnm01").className(Core_4010_Listnm01Info_Param.class).build();
        nameValueMap.put("DZ_INFO",list4010Nm01);
        //对公客户证件信息
//        AccountArrayNameAndClass list4010Nm=AccountArrayNameAndClass.builder().listName("listnm").className(Core_4010_ListnmInfo_param.class).build();
//        nameValueMap.put("DGKHZJ_INFO",list4010Nm);
        //对公电子信息
        AccountArrayNameAndClass list4010Nm02=AccountArrayNameAndClass.builder().listName("listnm02").className(Core_4010_Listnm02_Param.class).build();
        nameValueMap.put("DZDZLXXX_INFO",list4010Nm02);

        assembly(nameValueMap,infoInputs,mapPost);

    }

    /**
     * @Description: ${同业活期建立客户信息}
     * param [infoInputs, mapPost]
     * return void
     * author 北陈
     * date 2021/11/8 15:03
    */
    public static void addCoreNewAccountByPeers(List<InfoInput> infoInputs,Map mapPost){

    }

    /**
     *
     * @description {同业活期变更开户-变更客户信息}
     * @param infoInputs
     * @param mapPost
     * @return void
     * @author 北陈
     * @date 2021/11/11 0011 0:45
     **/
    public static void changeCoreNewAccountByPeers(List<InfoInput> infoInputs,Map mapPost){

    }
    /**
     * @Description: ${对公活期开户取数组}
     * param [infoInputs, mapPost]
     * return void
     * author 北陈
     * date 2021/11/7 15:33
    */
    public static void addCoreByOpenAccount(List<InfoInput> infoInputs,Map mapPost){

    }

    public static void assembly(Map<String, AccountArrayNameAndClass> map,List<InfoInput> infoInputs,Map mapPost){
        for (Map.Entry<String, AccountArrayNameAndClass> key:map.entrySet()){
            //外层容器名字
            String keyName = key.getKey();
            //添加的集合的名字和需要的类的class，用于获取所有字段名
            AccountArrayNameAndClass classMap = key.getValue();
            String listName = classMap.getListName();
            Class className = classMap.getClassName();
            //收集最后的数组集合
            ArrayList<Map<String, Object>> maps = new ArrayList<>();
            for (InfoInput infoInput:infoInputs){
                if (keyName.equals(infoInput.getInfoEnglishName())){
                    Map<String, Object> mapListFieldName = EscBodyMapTestUtil.getMap(className);
                    List<Map<String, Object>> result = getResult(infoInput);
                    for (Map mapNameValue:result){
                        //取出字段值赋再该类上的相关字段上
                        for (Map.Entry<String,Object> keyNameValue:mapListFieldName.entrySet()){
                            mapListFieldName.put(keyNameValue.getKey(),mapNameValue.get(keyNameValue.getKey()));
                        }
                        maps.add(mapListFieldName);
                    }
                }
            }
            if (maps.size()>0){
                //判断该集合名下是否存在了集合
                Object o = mapPost.get(listName);
                if (ObjectUtil.isNotNull(o)){
                    ArrayList<Map<String, Object>> mapArrayList=(ArrayList<Map<String, Object>>)o;
                    //将重新增加的集合加入该数组集合下
                    for (Map mapArray:maps){
                        mapArrayList.add(mapArray);
                    }
                    mapPost.put(listName,mapArrayList);
                }else {
                    //若为空，则直接加入
                    mapPost.put(listName,maps);
                }

            }

        }
    }
}
